package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.EduRelative;
import com.ruoyi.system.mapper.EduStudentMapper;
import com.ruoyi.system.domain.EduStudent;
import com.ruoyi.system.service.IEduStudentService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学生Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Service
public class EduStudentServiceImpl implements IEduStudentService 
{
    @Autowired
    private EduStudentMapper eduStudentMapper;

    /**
     * 查询学生
     * 
     * @param studentId 学生主键
     * @return 学生
     */
    @Override
    public EduStudent selectEduStudentByStudentId(Long studentId)
    {
        return eduStudentMapper.selectEduStudentByStudentId(studentId);
    }

    /**
     * 查询学生列表
     * 
     * @param eduStudent 学生
     * @return 学生
     */
    @Override
    public List<EduStudent> selectEduStudentList(EduStudent eduStudent)
    {
        return eduStudentMapper.selectEduStudentList(eduStudent);
    }

    /**
     * 新增学生
     * 
     * @param eduStudent 学生
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEduStudent(EduStudent eduStudent)
    {
        eduStudent.setCreateTime(DateUtils.getNowDate());
        int rows = eduStudentMapper.insertEduStudent(eduStudent);
        insertEduRelative(eduStudent);
        return rows;
    }

    /**
     * 修改学生
     * 
     * @param eduStudent 学生
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEduStudent(EduStudent eduStudent)
    {
        eduStudent.setUpdateTime(DateUtils.getNowDate());
        eduStudentMapper.deleteEduRelativeByStudentId(eduStudent.getStudentId());
        insertEduRelative(eduStudent);
        return eduStudentMapper.updateEduStudent(eduStudent);
    }

    /**
     * 批量删除学生
     * 
     * @param studentIds 需要删除的学生主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEduStudentByStudentIds(String studentIds)
    {
        eduStudentMapper.deleteEduRelativeByStudentIds(Convert.toStrArray(studentIds));
        return eduStudentMapper.deleteEduStudentByStudentIds(Convert.toStrArray(studentIds));
    }

    /**
     * 删除学生信息
     * 
     * @param studentId 学生主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEduStudentByStudentId(Long studentId)
    {
        eduStudentMapper.deleteEduRelativeByStudentId(studentId);
        return eduStudentMapper.deleteEduStudentByStudentId(studentId);
    }
    @Override
    public List<EduStudent> selectEduStudentAll() {
        return eduStudentMapper.selectEduStudentAll();
    }

    /**
     * 新增家庭成员信息
     * 
     * @param eduStudent 学生对象
     */
    public void insertEduRelative(EduStudent eduStudent)
    {
        List<EduRelative> eduRelativeList = eduStudent.getEduRelativeList();
        Long studentId = eduStudent.getStudentId();
        if (StringUtils.isNotNull(eduRelativeList))
        {
            List<EduRelative> list = new ArrayList<EduRelative>();
            for (EduRelative eduRelative : eduRelativeList)
            {
                eduRelative.setStudentId(studentId);
                list.add(eduRelative);
            }
            if (list.size() > 0)
            {
                eduStudentMapper.batchEduRelative(list);
            }
        }
    }
}
